package com.ycrlkj.android.modulescore.main.xpop;

import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.drawable.Drawable;
import android.view.View;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import com.blankj.utilcode.util.ToastUtils;
import com.lxj.easyadapter.EasyAdapter;
import com.lxj.easyadapter.MultiItemTypeAdapter;
import com.lxj.easyadapter.ViewHolder;
import com.lxj.xpopup.core.BottomPopupView;
import com.lxj.xpopup.util.XPopupUtils;
import com.lxj.xpopup.widget.VerticalRecyclerView;
import com.ycrlkj.android.modulescore.main.R;
import com.ycrlkj.android.modulescore.main.apiservice.FreightOrderApiService;
import com.ycrlkj.android.modulescore.main.entity.FreightOrder;
import com.ycrlkj.android.modulescore.main.entity.Query.FreightOrderQuery;
import com.ycrlkj.android.modulespublic.common.net.exception.ApiException;
import com.ycrlkj.android.modulespublic.common.net.exception.ErrorConsumer;
import com.ycrlkj.android.modulespublic.common.net.transformer.ResponseTransformer;
import java.util.List;
import javax.inject.Inject;
import dagger.hilt.android.AndroidEntryPoint;
import io.reactivex.functions.Consumer;

@AndroidEntryPoint
public class PopupBottomSelectFreightOrder extends BottomPopupView {

    @Inject
    FreightOrderApiService freightOrderApiService;

    private VerticalRecyclerView recyclerView;

    private EasyAdapter<FreightOrder> commonAdapter;

    private OnSelectedListener onSelectedListener;

    public PopupBottomSelectFreightOrder(@NonNull Context context, OnSelectedListener onSelectedListener) {
        super(context);
        this.setOnSelectedListener(onSelectedListener);
    }

    @Override
    protected int getImplLayoutId() {
        return R.layout.popup_bottom_freight_order_select;
    }

    @Override
    protected void onCreate() {
        super.onCreate();
        recyclerView = findViewById(R.id.recyclerView);

        findViewById(R.id.btn_close).setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                dismiss();
            }
        });

        getAll();
    }

    @SuppressLint("CheckResult")
    public void getAll() {
        FreightOrderQuery freightOrderQuery = new FreightOrderQuery();

        freightOrderApiService.getAll(freightOrderQuery)
                .compose(ResponseTransformer.obtain())
                .subscribe(new Consumer<List<FreightOrder>>() {
                               @Override
                               public void accept(List<FreightOrder> freightOrders) throws Exception {
                                   commonAdapter = new EasyAdapter<FreightOrder>(freightOrders, R.layout.item_pop_select_freight_order) {
                                       @Override
                                       protected void bind(@NonNull ViewHolder viewHolder, FreightOrder freightOrder, int position) {
                                           viewHolder.setText(R.id.tv_startAddress, freightOrder.getStartAddress())
                                                   .setText(R.id.tv_endAddress, freightOrder.getEndAddress())
                                                   .setText(R.id.tv_goodsTypeIdText, freightOrder.getGoodsTypeIdText())
                                                   .setText(R.id.tv_goodsCount, String.valueOf(freightOrder.getGoodsCount()))
                                                   .setText(R.id.tv_goodsCountUnit, freightOrder.getGoodsCountUnitText())
                                                   .setText(R.id.tv_consignmentDistance, String.valueOf(freightOrder.getConsignmentDistance()))
                                                   .setText(R.id.tv_deliveryTime, freightOrder.getDeliveryTime());

                                           String vehiclePlateNum = freightOrder.getVehiclePlateNum();
                                           Integer vehiclePlateColor = freightOrder.getVehiclePlateColor();

                                           Drawable bg_warn = getResources().getDrawable(R.drawable.m_bg_warn);
                                           Drawable bg_primary = getResources().getDrawable(R.drawable.m_bg_primary);
                                           Drawable bg_success = getResources().getDrawable(R.drawable.m_bg_success);
                                           Drawable bg_info = getResources().getDrawable(R.drawable.m_bg_info);

                                           TextView tv_plateNum = viewHolder.getView(R.id.tv_plateNum);
                                           tv_plateNum.setText(vehiclePlateNum);

                                           if(vehiclePlateColor == 1){
                                               tv_plateNum.setBackground(bg_warn);
                                           }else if(vehiclePlateColor == 2){
                                               tv_plateNum.setBackground(bg_primary);
                                           }else if(vehiclePlateColor == 3){
                                               tv_plateNum.setBackground(bg_success);
                                           }else{
                                               tv_plateNum.setBackground(bg_info);
                                           }
                                       }
                                   };

                                   //item点击监听
                                   commonAdapter.setOnItemClickListener(new MultiItemTypeAdapter.SimpleOnItemClickListener() {
                                       @Override
                                       public void onItemClick(View view, RecyclerView.ViewHolder holder, int position) {
                                           //不要直接 dismiss(); 会导致消失动画未执行完就跳转界面
                                           //使用 dismissWith() 等消失动画执行完毕
                                           dismissWith(new Runnable() {
                                               @Override
                                               public void run() {
                                                   onSelectedListener.selected(freightOrders.get(position));
                                               }
                                           });
                                       }
                                   });
                                   recyclerView.setAdapter(commonAdapter);
                               }
                           },
                        new ErrorConsumer() {
                            @Override
                            protected void error(ApiException e) {
                                ToastUtils.showLong(e.getMessage());
                            }
                        });
    }


    public interface OnSelectedListener {
        void selected(FreightOrder freightOrder);
    }

    public void setOnSelectedListener(OnSelectedListener onSelectedListener) {
        this.onSelectedListener = onSelectedListener;
    }

    @Override
    protected void onShow() {
        super.onShow();
    }

    @Override
    protected void onDismiss() {
        super.onDismiss();
    }

    @Override
    protected int getMaxHeight() {
        return (int) (XPopupUtils.getScreenHeight(getContext()) * .7f);
    }

}